﻿namespace CTP.CRM.Core
{
    public static class CTPABPCore
    {
        internal static IServiceProvider _internalProvider;

        internal static IConfiguration _internalConfiguration;

        internal static IServiceCollection _internalServices;

        //
        // 摘要:
        //     获取HTTP请求上下文对象
        public static HttpContext HttpContext
        {
            get
            {
                IServiceProvider internalProvider = _internalProvider;
                return (internalProvider == null) ? null : ServiceProviderServiceExtensions.GetService<IHttpContextAccessor>(internalProvider)?.HttpContext;
            }
        }

        //
        // 摘要:
        //     获取配置容器
        public static IConfiguration Configuration => _internalConfiguration;

        //
        // 摘要:
        //     获取服务容器
        public static IServiceProvider ServiceProvider => _internalProvider;

        //
        // 摘要:
        //     初始化函数
        //
        // 参数:
        //   builder:
        internal static void ConfigureApplication(IWebHostBuilder builder)
        {
            builder.ConfigureServices(delegate (WebHostBuilderContext hostContext, IServiceCollection services)
            {
                _internalConfiguration = hostContext.Configuration;
                _internalServices = services;
                services.AddHttpContextAccessor();
                _internalProvider = services.BuildServiceProvider();
            });
        }
    }
}
